﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Control;
using TelnetRouterApplication.Model;
using TelnetRouterApplication.Utils;

namespace TelnetRouterApplication.View
{
    public partial class VLANForm : UserControl
    {
        private MainForm _main;
        public MainForm Main
        {
            get { return _main; }
            set { _main = value; }
        }

        private List<VLAN> _VLANlist;
        public List<VLAN> VLANlist
        {
            get { return _VLANlist; }
            set { _VLANlist = value; }
        }

        public VLANForm() : this(null) { }

        public VLANForm(MainForm main)
        {
            Main = main;

            InitializeComponent();

            RefreshList();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            string name = tbName.Text;
            string number = tbNumber.Text;

            VLAN vlan = new VLAN(name, number);

            CommandHandler.GetInstance().AddVLAN(vlan);

            RefreshList();
        }

        private void removeBtn_Click(object sender, EventArgs e)
        {
            VLAN vlanToDelete = (VLAN)vlanListBox.SelectedItem;
            if (vlanToDelete != null)
            {
                CommandHandler.GetInstance().DeleteVLAN(vlanToDelete);
            }

            RefreshList();
        }

        private void RefreshList()
        {
            VLANlist = CommandHandler.GetInstance().getListVLAN();

            vlanListBox.BeginUpdate();
            vlanListBox.Items.Clear();
            foreach (VLAN vlan in VLANlist)
            {
                vlanListBox.Items.Add(vlan);
            }
            vlanListBox.EndUpdate();
        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            //Achei desnecessário checar se o settings é nulo

            string log = "";
            foreach (VLAN vlan in VLANlist)
            {
                log += vlan.ToString() + "\r\n";
            }

            string path = IOUtils.SaveFileDialogShow(Main, "Text Files|*.txt");
            IOUtils.CreateTxtFile(path, log);
            MessageBox.Show("Log exported!");
        }

        private void importBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Import VLANs will add differents VLANs from the imported file. Continue?", "Import", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string path = IOUtils.OpenFileDialogShow(Main);
                string[] importedSettings = IOUtils.ReadTxtFile(path);

                //Precisa fazer toda a verificação

                string[] parameters = null;

                for (int i = 0; i < importedSettings.Length; i++)
                {
                    parameters = importedSettings[i].Split('@');

                    if (parameters.Length > 1)
                    {
                        string name = parameters[0];
                        string number = parameters[1];

                        VLAN vlan = new VLAN(name, number);

                        if (!VLANlist.Contains(vlan))
                        {
                            CommandHandler.GetInstance().AddVLAN(vlan);
                        }
                    }
                }

                MessageBox.Show("Import completed!");
                RefreshList();
            }
        }
    }
}
